//kotori和迷宫
import java.util.;

注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       int n = in.nextInt();
       int m = in.nextInt();
       char[][] arr = new char[n][m];
       boolean[][] flag = new boolean[n][m];
       int[][] dp = new int[n][m];

       for (int i = 0; i  n; i++) {
           arr[i] = in.next().toCharArray();
       }
       int x = 0;
       int y = 0;
       for (int i = 0; i  n; i++) {
           for (int j = 0; j  m; j++) {
               if (arr[i][j] == 'k') {
                   x = i;
                   y = j;
                   i = n;
                   break;
               }
           }
       }
       QueueData queue = new LinkedList();
       queue.add(new Data(x, y, 0));
       flag[x][y] = true;
       
       while (!queue.isEmpty()) {
           Data data = queue.poll();
           x = data.x;
           y = data.y;
           int count = data.count;
           
           count++;
           x++;
           if (x  n && flag[x][y] == false && (arr[x][y] == '.'  arr[x][y] == 'e')) {
               flag[x][y] = true;
               dp[x][y] = count;
               if (arr[x][y] == '.')
               queue.add(new Data(x, y, count));
           }
           x--;
           x--;
           if (x = 0 && flag[x][y] == false && (arr[x][y] == '.'  arr[x][y] == 'e')) {
               flag[x][y] = true;
               dp[x][y] = count;
               if (arr[x][y] == '.')
               queue.add(new Data(x, y, count));
           }
           x++;
           y++;
           if (y  m && flag[x][y] == false && (arr[x][y] == '.'  arr[x][y] == 'e')) {
               flag[x][y] = true;
               dp[x][y] = count;
               if (arr[x][y] == '.')
               queue.add(new Data(x, y, count));
           }
           y--;
           y--;
           if (y = 0 && flag[x][y] == false && (arr[x][y] == '.'  arr[x][y] == 'e')) {
               flag[x][y] = true;
               dp[x][y] = count;
               if (arr[x][y] == '.')
               queue.add(new Data(x, y, count));
           }
       }
       int ret = 0;
       int min = Integer.MAX_VALUE;
       for (int i = 0; i  n; i++) {
           for (int j = 0; j  m; j++) {
               if (arr[i][j] == 'e' && dp[i][j] != 0) {
                   ret++;
                   min = Math.min(dp[i][j], min);
               }
           }
       }
       if (ret == 0) {
           System.out.println(-1);
       } else {
           System.out.println(ret+ +min);
       }
   }
}
class Data {
   int x;
   int y;
   int count;
   public Data(int x, int y, int count) {
       this.x = x;
       this.y = y;
       this.count = count;
   }
}